﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Srvtools;

public partial class WorkTime_DepartmentDList : System.Web.UI.UserControl
{
    private string FirstHtml_Lev1 = "├";
    private string _SpaceHtml = "─";

    private string [] _OptionStyle = new string [4]{"Background:#CCCCCC;","Background:#DDDDDD;","Background:#EEEEEE;","Background:#f6f6f6;"};

    private string _TopDepartmentId = "";
    private string _BindDepmanetId = "";
    private string _IsAfterBind = "";

    private DataTable _allDpt = null;

    /// <summary>
    /// 在有权限状态下的最顶层的部门编号
    /// </summary>
    public string TopDepartmentId
    {
        get
        {
            return _TopDepartmentId;
        }
        set
        {
            _TopDepartmentId = value;
        }
    }

    public string SelectDepmanetIdHid
    {
        get
        {
            return HidValue.Value;
        }
        set
        {
            HidValue.Value = value;
        }
    }

    /// <summary>
    /// 当前选择的部门编号
    /// </summary>
    public string SelectDepmanetId
    {
        get
        {
            return WebDropDownList1.SelectedValue;
        }
        set
        {
            WebDropDownList1.SelectedValue = value;
        }
    }

    /// <summary>
    /// 当前选择的部门编号
    /// </summary>
    public string IsAfterBind
    {
        get
        {
            return _IsAfterBind;
        }
        set
        {
            _IsAfterBind = value;
        }
    }

    public string BindDepmanetId
    {
        get
        {
            return _BindDepmanetId;
        }
        set
        {
            _BindDepmanetId = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
       if (!Page.IsPostBack)
       {

           Bind();
           if (IsAfterBind != "")
           {
               WebDropDownList1.SelectedValue = IsAfterBind;
           }
        }//if not ispostback
    }

    public DataTable AllDpt
    {
        get
        {
            return _allDpt;
        }
        set
        {
            _allDpt = value;
        }
    }

    public void Bind()
    {
        if (WebDropDownList1.Items.Count > 0)
        {
            return;
        }

        ListItem dt = new ListItem();
        dt.Text = "──Hyperion──";
        dt.Value = "0";

        string where = "";
        if (AllDpt != null)
        {
            //where = " And DepartmentId like '" + ParentDpt + "%' ";
        }
        else
        {
            WebDropDownList1.Items.Add(dt);
        }

        object[] oj = Srvtools.CliUtils.CallMethod("SDepartment", "GetDptIdDDL", new object[] { where });

        DataTable Dep = ((DataSet)oj[1]).Tables[0];


        //dt.Attributes.Add("style", "Background:#CCCCCC;");
        

        Hashtable DepartmentListL = new Hashtable(); //不分层次表
        Hashtable DepartmentListT = new Hashtable(); //树形表
        Hashtable Dtt;
        Hashtable DttA;
        Hashtable DttB;

        string DepartmentId = "";
        string ParentDepartId = "";
        string DepartmentName = "";

        for (int i = 0; i < Dep.Rows.Count; i++)
        {
            DepartmentId = Dep.Rows[i]["DepartmentId"].ToString().Trim();
            ParentDepartId = Dep.Rows[i]["ParentDepartId"].ToString().Trim();
            DepartmentName = Dep.Rows[i]["DepartmentName"].ToString();

            ArrayList Dtp = new ArrayList();
            Dtt = new Hashtable();
            // Dtp.Add(new object[] { DepartmentId, DepartmentName, Dtt });
            Dtp.Add(DepartmentId);
            Dtp.Add(DepartmentName);
            Dtp.Add(Dtt);
            DepartmentListL.Add(DepartmentId, Dtp);

            DttA = new Hashtable();
            DttA.Add(DepartmentId, Dtp);

            if (DepartmentListL.ContainsKey(ParentDepartId) == true)
            {
                ArrayList DtpB = (ArrayList)DepartmentListL[ParentDepartId];
                DttB = (Hashtable)DtpB[2];
                DttB.Add(DepartmentId, DttA);
            }
            else
            {
                //if (AllDpt != null)
                //{
                //    if (AllDpt.Select("DepartmentId=" + DepartmentId).Length > 0)
                //    {
                //        DepartmentListT.Add(DepartmentId, DttA);
                //    }
                //}
                //else
                //{
                    DepartmentListT.Add(DepartmentId, DttA);
                //}
                
            }

        }//for

        // 判断是否有权限限制
        if (_TopDepartmentId.Length == 0)
        {
            SetDropListData(ref WebDropDownList1, ref DepartmentListT, 0,AllDpt);
        }
        else
        {
            if (DepartmentListL.ContainsKey(_TopDepartmentId) == true)
            {
                ArrayList DtpB = (ArrayList)DepartmentListL[_TopDepartmentId];
                DepartmentId = (string)DtpB[0];
                DepartmentName = (string)DtpB[1];
                DttB = (Hashtable)DtpB[2];

                DepartmentName = FirstHtml_Lev1 + DepartmentName;
                ListItem Ldt = new ListItem();
                Ldt.Text = DepartmentName;
                Ldt.Value = DepartmentId;

                dt.Attributes.Add("style", _OptionStyle[0]);
                WebDropDownList1.Items.Add(Ldt);

                SetDropListData(ref WebDropDownList1, ref DttB, 1,AllDpt);
            }

        }

        LitTextBox.Visible = false;
    }

    public void ddlSelectValue(string Did)
    {
        
        Bind();

        WebDropDownList1.SelectedValue = Did;
        LitTextBox.Visible = false;
        WebDropDownList1.Visible = true;
    }

    public void ShowTextOnly(string Did)
    {
        
        Bind();

        WebDropDownList1.SelectedValue = Did;
        string Text = WebDropDownList1.SelectedItem.Text ;

        WebDropDownList1.Visible = false;
        LitTextBox.Visible = true;

        Text = Text.Replace("├","");
        Text = Text.Replace("─","");

      
        LitTextBox.Text = Text;

        HidValue.Value = Did;
    }


    private void SetDropListData(ref System.Web.UI.WebControls.DropDownList InDropDownList, ref Hashtable IndataCol, int DataLevel,DataTable AllDpt)
    {
        ICollection valueColl = IndataCol.Values;
        foreach (Hashtable DtpC in valueColl)
        {
            ICollection valueColls = DtpC.Values;
            foreach (ArrayList DtpCC in valueColls)
            {
                //  ArrayList DtpC = (ArrayList)IndataCol[ii];
                string DepartmentId = (string)DtpCC[0];
                string DepartmentName = (string)DtpCC[1];
                Hashtable Dtt = (Hashtable)DtpCC[2];
                int DLs = DataLevel;

                string AddSpace = "";
                for (int i = 0; i < DLs; i++)
                {
                    AddSpace = _SpaceHtml + AddSpace;
                }
                DepartmentName = FirstHtml_Lev1 + AddSpace + DepartmentName;

                ListItem dt = new ListItem();
                dt.Text = DepartmentName;
                dt.Value = DepartmentId;
                if (DataLevel > 3) DLs = 3;
                dt.Attributes.Add("style", _OptionStyle[DLs]);

                
                if (AllDpt != null)
                {
                    for (int i = dt.Value.Length ; i >= 2; i = i / 2 )
                    {
                        if(AllDpt.Select("DepartmentId like '" + dt.Value.Substring(0,i) + "'").Length > 0)
                        {
                            WebDropDownList1.Items.Add(dt);
                        }
                    }
                   
                }
                else
                {
                    WebDropDownList1.Items.Add(dt);
                }
                if (Dtt.Count > 0)
                {
                    SetDropListData(ref InDropDownList, ref Dtt, DataLevel + 1,AllDpt);
                }

            }
        }
    }
}
